package com.yunkeji.api.wall.mapper;

import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.api.wall.bo.PageLikeWallBo;
import com.yunkeji.api.wall.vo.LikeWallVo;
import com.yunkeji.dao.mapper.LikeWallMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

public interface ApiLikeWallMapper extends LikeWallMapper {

    /**
     * 贴贴墙列表
     *
     * @author 老张
     */
    @Select(
            "<script>"
                    + "SELECT  "
                    + "  wall.*,  "
                    + "  COUNT( detail.wall_id ) like_num  "
                    + "  FROM    "
                    + "  ykj_like_wall wall  "
                    + "  left JOIN ykj_like_wall_detail detail ON wall.id = detail.wall_id  "
                    + "  and wall.status=2 "
                    + "  LEFT JOIN ykj_app_user_ext ext ON ext.id = detail.user_id   "
                    + " where 1=1  "
                    // 贴贴墙类型 1=心愿墙 2= 缘分墙 3= 励志墙 4=爱好墙 5=心墙墙
                    + "<if test='bo.type!=null'>"
                    + " and wall.type= #{bo.type} "
                    + "</if>"
                    // uid
                    + "<if test='uid!=null'>"
                    + " and wall.user_id= #{uid} "
                    + "</if>"
                    // 1待审核2审核通过3审核拒绝
                    + "<if test='status!=null'>"
                    + " and wall.status= #{status} "
                    + "</if>"
                    + " GROUP BY wall.id  "
                    + " order by wall.create_time desc"
                    + "  "
                    + "</script>")
    @Result(
            property = "headInfo",
            column = "head_info",
            jdbcType = JdbcType.BLOB,
            typeHandler = JacksonTypeHandler.class)
    Page<LikeWallVo> list(
            @Param("page") Page<LikeWallVo> buildPageQuery,
            @Param("bo") PageLikeWallBo bo,
            @Param("uid") Long uid,
            @Param("status") Integer status);

    /**
     * 增加点赞数量
     *
     * @author 老张
     * @param id
     */
    @Update("UPDATE ykj_like_wall SET nums = nums+1 WHERE id =#{id}")
    Integer addLikes(@Param("id") Long id);

    /**
     * 减少点赞数量
     *
     * @author 老张
     * @param id
     */
    @Update("UPDATE ykj_like_wall SET nums = nums-1 WHERE id =#{id}")
    Integer subLikes(@Param("id") Long id);
}
